package com.net.shop.car.manager.api.volley.response;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.net.shop.car.manager.api.model.HeadPicture;
import com.net.shop.car.manager.api.model.VersionInfo;
import com.net.shop.car.manager.api.volley.Response;
import com.net.shop.car.manager.utils.Constants;
import com.net.shop.car.manager.utils.LogUtils;
import com.net.shop.car.manager.utils.StringUtils;

/**
 * @author Deve Xu
 *
 */
public class GetHeadPicturesResponse extends Response {
	
	/**
	 * 
	 */
	private List<HeadPicture> headPictures;
	
	/**
	 * 
	 */
	private VersionInfo versionInfo;

	/**
	 * @return the headPictures
	 */
	public List<HeadPicture> getHeadPictures() {
		return headPictures;
	}

	/**
	 * 
	 */
	public GetHeadPicturesResponse() {
		super(Constants.GET_HEAD_PICTURES);
	}
	
	@Override
	public void parseContent(String content,int index) {
		if (index == 0) {
			parseVersion(content);
		} else {
			parsePicInfo(content);
		}
	}
	
	/**
	 * @param content
	 */
	private void parsePicInfo(String content) {
		try {
			JSONArray jsonArray = new JSONArray(content);
			headPictures = new ArrayList<HeadPicture>();
			for (int i = 0; i < jsonArray.length(); i++) {
				HeadPicture headPicture = new HeadPicture();
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				headPicture.setContent(StringUtils.filterNull(jsonObject.getString("CONTENT")));
				headPicture.setMinPrice(StringUtils.filterNull(jsonObject.getString("MIN_PRICE")));
				headPicture.setBeginDate(StringUtils.filterNull(jsonObject.getString("BEGIN_DATE")));
				headPicture.setEndDate(StringUtils.filterNull(jsonObject.getString("END_DATE")));
				headPicture.setLogoUrl(StringUtils.filterNull(jsonObject.getString("LOGO_URL")));
				headPicture.setTitle(StringUtils.filterNull(jsonObject.getString("TITLE")));
				headPicture.setId(StringUtils.filterNull(jsonObject.getString("ID")));
				String isApply = jsonObject.getString("IS_APPLY");
				headPicture.setApply("yes".equals(isApply));
				headPictures.add(headPicture);
			}
		} catch (JSONException e) {
			LogUtils.e(e.getMessage());
		}
	}
	
	/**
	 * @param content String
	 */
	private void parseVersion(String content) {
		try {
			versionInfo = new VersionInfo();
			JSONObject jsonObject = new JSONObject(content);
			versionInfo.setId(jsonObject.getString("ID"));
			versionInfo.setVersionName(StringUtils.filterNull(jsonObject.getString("VERSION_NAME")));
			versionInfo.setVersionId(StringUtils.filterNull(jsonObject.getString("VERSION_ID")));
			versionInfo.setVersionUrl(StringUtils.filterNull(jsonObject.getString("VERSION_URL")));
			versionInfo.setCreateDate(StringUtils.filterNull(jsonObject.getString("CREATE_DATE")));
			versionInfo.setModifyDate(StringUtils.filterNull(jsonObject.getString("MODIFY_DATE")));
		} catch (JSONException e) {
			LogUtils.e(e.getMessage());
		}
	}
	
	@Override
	public String[] getContentTags() {
		return new String[]{"version","picinfo"};
	}

	/**
	 * @return the versionInfo
	 */
	public VersionInfo getVersionInfo() {
		return versionInfo;
	}
}
